<template>
  <div>
    <account-card title="商品列表">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="所属分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="商品图片">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="props.row.imgUrl"
                ></el-image>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.ctime }}</span>
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="食品销量">
                <span>{{ props.row.sellCount }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="name"></el-table-column>
        <el-table-column label="所属分类" prop="category"></el-table-column>
        <el-table-column label="商品价格" prop="price"></el-table-column>
        <el-table-column label="商品图片">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              v-model="scope.row"
              :src="scope.row.imgUrl"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品描述" prop="goodsDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="changegoods(scope.row)"
              size="mini"
              >编辑</el-button
            >
            <el-popconfirm
              confirm-button-text="好的"
              cancel-button-text="不用了"
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="handleDelete(scope.row)"
            >
              <el-button
                size="mini"
                type="danger"
                class="delbtn"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="sizechange"
        @current-change="pagechange"
        :current-page="currentPage"
        :page-sizes="[1, 3, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 模态框 -->
      <el-dialog width="350px" title="商品修改" :visible.sync="dialogVisible">
        <el-form size="mini" :model="moData">
          <el-form-item label="商品名称" :label-width="formLabelWidth">
            <el-input v-model="moData.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="所属分类" :label-width="formLabelWidth">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in category"
                :key="item.cateName"
                :label="item.cateName"
                :value="item.cateName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格" :label-width="formLabelWidth">
            <el-input v-model="moData.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imgUrl" :src="imgUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品描述" :label-width="formLabelWidth">
            <el-input v-model="moData.goodsDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEdit">确 定</el-button>
        </div>
      </el-dialog>
    </account-card>
  </div>
</template>

<script>
import { list, goodsdel, goodsedit, goodscategories } from "@/apis/goods.js";
import AccountCard from "../../../components/accountcard/AccountCard.vue";
export default {
  data() {
    return {
      API: "http://127.0.0.1:5000/upload/imgs/goods_img/",
      tableData: [],
      currentPage: 1, // 当前页
      pageSize: 5, //每页条数
      total: 0, // 总数据条数
      dialogVisible: false,
      formLabelWidth: "100px",
      moData: {
        name: "",
        price: "",
        goodsDesc: "",
      },
      value: "",
      imgUrl: "",
      category: [],
    };
  },
  components: {
    AccountCard,
  },
  methods: {
    getData() {
      list({ currentPage: this.currentPage, pageSize: this.pageSize })
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //图片回填
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imgUrl = URL.createObjectURL(file.raw);
      this.newimg = res.imgUrl;
    },
    //当前页
    sizechange(val) {
      this.pageSize = val;
      // console.log(val);
      this.getData();
    },
    //切换页面
    pagechange(val) {
      this.currentPage = val;
      this.getData();
    },
    //删除商品
    handleDelete(row) {
      goodsdel({ id: row.id })
        .then((res) => {
          console.log(res);
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //修改商品
    changegoods(row) {
      this.dialogVisible = true;
      this.moData.name = row.name;
      this.moData.price = row.price;
      this.moData.goodsDesc = row.goodsDesc;
      this.imgUrl = row.imgUrl;
      this.goodsid = row.id;
      this.value=row.category
      //默认值
      this.newimg = row.imgUrl.substring(row.imgUrl.lastIndexOf("/") + 1)
      goodscategories().then((res) => {
        // console.log(res.data);
        this.category = res.data.categories;
      });
    },
    //保存修改
    saveEdit() {
      this.dialogVisible = false;
      goodsedit({
        name: this.moData.name,
        category: this.value,
        price: this.moData.price,
        imgUrl: this.newimg,
        goodsDesc: this.moData.goodsDesc,
        id: this.goodsid,
      })
        .then((res) => {
          console.log(res.data);
          this.$message({
            message: "恭喜你，修改成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log(err);
          this.$message.error("修改失败");
        });
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.goods-list-img {
  width: 40px;
  height: 40px;
}
.avatar {
  width: 100px;
  height: 100px;
}
</style>